package com.ctgu.chatapp.view.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.ctgu.chatapp.databinding.ItemSearchfriendBinding
import com.ctgu.chatapp.showPojo.SearchFriend
import com.ctgu.chatapp.utils.Utils
import com.ctgu.chatapp.view.DialogFragment.AddFriend

class SearchFriendAdapter(
    var list: List<SearchFriend>,
    private val fragmentManager: FragmentManager
) :
    RecyclerView.Adapter<SearchFriendAdapter.ViewHolder>() {

    private lateinit var mRecycleView: RecyclerView

    class ViewHolder(val binding: ItemSearchfriendBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ) = run {
        val binding =
            ItemSearchfriendBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.search = list[position]
        holder.binding.accept.setOnClickListener {
            val show = AddFriend(Utils.chatID, list[position].ChatID)
            show.showNow(fragmentManager, "添加好友")
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.mRecycleView = recyclerView
    }

    override fun getItemCount() = list.size

    @SuppressLint("NotifyDataSetChanged")
    fun changeList(list: List<SearchFriend>) {
        this.list = list
        notifyDataSetChanged()
    }
}